<template>
  <div class="app-container">

    <div class="filter-container">
      <el-date-picker
        v-model="date_value"
        range-separator="至"
        start-placeholder="开始时间"
        end-placeholder="结束时间"
        type="datetimerange"
        :picker-options="pickerOptions"
        value-format="yyyy-MM-dd#HH:mm:ss"
        class="filter-item">
      </el-date-picker>

      <el-checkbox-group v-model="checkedStates" @change="handleCheckedStateChange" style="margin-left:10px;">
        <el-checkbox-button type="success" v-for="state in states" :label="state.value" :key="state.value">{{state.label}}</el-checkbox-button>
      </el-checkbox-group>

      <el-button style="margin-left: 10px;" class="filter-item" type="primary" @click="handleQuery">查询</el-button>
      <el-button style="margin-left: 10px;" class="filter-item" type="primary" @click="goPrint">打印</el-button>
    </div>

    <el-table
      v-loading="listLoading"
      :data="list"
      border
      fit
      highlight-current-row
      style="width: 100%;"
    >
      <el-table-column label="一级分类" width="150">
        <template slot-scope="{row}">
          <el-link type="primary">{{ row.parent.name }}</el-link>
        </template>
      </el-table-column>
      <el-table-column label="二级分类" width="150">
        <template slot-scope="{row}">
          <el-link type="danger">{{ row.name }}</el-link>
        </template>
      </el-table-column>
      <el-table-column label="拣货列表">
        <template slot-scope="{row}">
          <div v-for="item in row.items" :key="item.id" class="item-line">
            <div>
              <el-tag>{{item.product_name}}</el-tag>
              <el-tag type="success">规格：{{ item.name }}</el-tag>
            </div>
            <div style="width: 180px;display: flex;justify-content: space-between;">
              <el-tag type="danger">数量：{{ item.quantity }}</el-tag>
              <el-tag type="danger" v-if="item.zone">库位：{{ item.zone + item.shelf + '-' + item.floor + '-' + item.number }}</el-tag>
            </div>
          </div>
        </template>
      </el-table-column>
    </el-table>

  </div>
</template>

<script>
import { getList } from '@/api/picking-order'
export default {
  name: 'Picking-Order',
  data() {
    return {
      list: null,
      listLoading: false,
      date_value: null,
      pickerOptions: {
        shortcuts: [{
          text: '两天内',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 1);
            const str = start.getFullYear() + '/' + (start.getMonth() + 1) + '/' + start.getDate() + ' 00:00:00'
            const start_ = new Date(str);
            end.setTime(start_.getTime() + 3600 * 1000 * 24 * 2 - 1000)
            picker.$emit('pick', [start_, end]);
          }
        }]
      },
      states:[
        {label:'待发货',value:'SHIPPING'},
        {label:'待收货',value:'SHIPPED'},
        {label:'已收货',value:'DELIVERED'},
        {label:'已完成',value:'FINISHED'}
      ],
      checkedStates:['SHIPPING']
    }
  },
  methods: {
    getList() {
      this.listLoading = true
      let query = {
        from_date:this.date_value[0].split('#')[0],
        from_time:this.date_value[0].split('#')[1],
        to_date:this.date_value[1].split('#')[0],
        to_time:this.date_value[1].split('#')[1]
      }
      let states = this.checkedStates
      getList(query,states).then(response => {
        this.list = response.categories
        this.listLoading = false
      }).catch(error => {
        this.list = []
        this.listLoading = false
        Promise.reject(error)
      })
    },
    handleQuery:function(){
      if(!this.date_value){
        this.$message.error('请选择开始时间和结束时间');
        return
      }
      this.getList()
    },
    goPrint:function(){
      if(!this.date_value || this.date_value.length < 2){
        return
      }
      let states = this.checkedStates.join(',')
      let routeData = this.$router.resolve({
        path: "/print/picking-order",
        query:{
          from_date:this.date_value[0].split('#')[0],
          to_date:this.date_value[1].split('#')[0],
          states:states
        }
      });
      window.open(routeData.href, '_blank');
    },
    handleCheckedStateChange(values){
      console.log(values)
    }
  }
}
</script>

<style>
  .filter-container{
    display: flex;
  }
  .item-line{
    display: flex;
    justify-content: space-between;
    align-items: stretch;
    width: 500px;
    border:1px solid white;
  }
  .item-line:hover{
    border:1px solid #C03639;
  }
</style>
